<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Csr extends CI_Controller {

    function __construct() {
        parent::__construct();

        $this->title = "SMILE | Selamat datang, telkom siap melayani Anda!";

        $this->load->helper(array('date', 'url'));
        $this->layout = "admin";

        $this->scripts = array("jquery", "jquery-ui", "jquery.dataTables.min");
        $this->styles = array("jquery-ui", "demo_table", "useradmin");
    }

    public function index() {
        if ($this->session->userdata('is_super_admin')) {
            
        } else if ($this->session->userdata('is_admin')) {
            redirect('admin/site/');
        } else if ($this->session->userdata('is_pic')) {
            redirect('admin/statistic');
        } else {
            redirect('admin/site/login');
        }

        $menudata['is_admin'] = $this->session->userdata('is_admin');
        $menudata['is_pic'] = $this->session->userdata('is_pic');
        $menudata['is_super_admin'] = $this->session->userdata('is_super_admin');
        $this->menubar = $this->load->view('admin/menubar', $menudata);

        $data["csrs"] = $this->csrmodel->get_all_csr()->result_array();
        $data['plasas'] = $this->plasamodel->get_all_plasa_simple()->result_array();

        $this->load->view("admin/csr_management", $data);

        //$data["data"] = $this->csrmodel->get_all_csr();
        //$this->load->view("admin/dummy", $data);
    }


    function _upload($csr_id) {
		
		if($_FILES){
			//print_r($_FILES['image']);
			$metadata = getImageSize($_FILES['image']['tmp_name']);
			
			$file_name = $csr_id . CSR_EXTENSION;
			$type_text = explode('/', $_FILES['image']['type']);
			$ext = $type_text[1];

			$config["file_name"] = $file_name;
			$config['upload_path'] = PATH_TO_CSR_PHOTO;
			$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg|GIF|JPG|PNG|BMP|JPEG';
			//$config['max_size'] = 2048;
			$config['max_width'] = IMAGE_MAX_WIDTH;
			$config['max_height'] = IMAGE_MAX_HEIGHT;
			$config['overwrite'] = TRUE;

			$this->load->library('upload', $config);

			if (!$this->upload->do_upload('image')) {
				$this->session->set_flashdata('upload_msg', $this->upload->display_errors());
				return false;
			} else {
				$this->_resize($file_name, $metadata);
			}
			return true;
		}else{
			return false;
		}
    }

    function _resize($filename, $metadata) {
        $config['image_library'] = 'gd2';
        $config['source_image'] = PATH_TO_CSR_PHOTO . DIRECTORY_SEPARATOR. $filename;
        $config['maintain_ratio'] = TRUE;
		//echo $metadata[0] .' + '.$metadata[1];
        if($metadata[0] < $metadata[1]) {
			$config['width'] = IMAGE_RESIZE;
			$config['height'] = $metadata[1]/$metadata[0]*IMAGE_RESIZE;
			//echo 'width:'.$config['width'].'-height:'.$config['height'];
		}else {
			$config['height'] = IMAGE_RESIZE;
			$config['width'] = $metadata[0]/$metadata[1]*IMAGE_RESIZE;
			//echo 'width:'.$config['width'].'-height:'.$config['height'];
		}

        $this->load->library('image_lib', $config);
        if (!$this->image_lib->resize()) {
            $this->session->set_flashdata('upload_msg', $this->image_lib->display_errors());
        }
        return TRUE;
    }
	
    public function add() {
        $this->layout = false;

        $name = $this->input->post('name');
        $contact = $this->input->post('contact');
        $plasa_id = $this->input->post('plasa_id');

        $new_csr_id = $this->csrmodel->add_csr($name, $contact, $plasa_id);

		if($_FILES){
			$upload_msg = ($this->_upload($new_csr_id)) ? $this->session->flashdata('upload_msg') : 'Tidak ada foto yang terupload.';
			$this->session->set_flashdata('form', array('success' => 'true', 'message' => 'Tambah CSR Berhasil. ' . $upload_msg));
		}
		
        redirect('/admin/csr/index');
    }

    public function getcsr($csr_id) {
        $this->layout = false;

        echo json_encode($this->csrmodel->get_csr_info($csr_id)->row_array());
    }

    public function update() {
        $this->layout = false;

        $csr_id = $this->input->post('csr_id');
        $name = $this->input->post('name');
        $contact = $this->input->post('contact');
        $plasa_id = $this->input->post('plasa_id');

        $this->csrmodel->update_csr($csr_id, $name, $contact, $plasa_id);
        //echo $csr_id;
        $upload_msg = ($this->_upload($csr_id)) ? $this->session->flashdata('upload_msg') : 'Tidak ada foto yang terupload.';
        $this->session->set_flashdata('form', array('success' => 'true', 'message' => 'Tambah CSR Berhasil. ' . $upload_msg));

        redirect('/admin/csr/index');
        //echo $csr_id;
    }

    public function delete() {
        $this->layout = false;
        $csr_id = $this->input->post('csr_id');
        $this->csrmodel->delete_csr($csr_id);

        $this->load->helper("file");
        unlink(PATH_TO_CSR_PHOTO . DIRECTORY_SEPARATOR . $csr_id . CSR_EXTENSION);
    }

    public function get_csr_by_plasa() {
        $this->layout = false;
        //$this->input->post('plasa_id');
        //echo json_encode($this->csrmodel->get_all_csr_by_plasa($this->input->post('plasa_id'))->row_array());
        $result = $this->csrmodel->get_all_csr_by_plasa($this->input->post('plasa_id'));
        echo json_encode($result->result_array());
    }

    public function get_all_csr() {
        $this->layout = false;

        $result = $this->csrmodel->get_all_csr();
        echo json_encode($result->result_array());
    }

}

?>
